﻿//Copyright (c) Microsoft Corporation.  All rights reserved.

using System;
using System.Collections.Generic;
using System.Linq;

namespace Microsoft.Ted.Wacel.Base
{
    public class Dictionary<T> : CloudDataStructure
    {
        public Dictionary(string name, IDataProvider dataProvider, ILockProvider lockProvider)
            :base(name, dataProvider, lockProvider)
        {
        }
        public virtual void Add(string key, T obj, string partition = null)
        {
            Put(key, obj, partition);
        }
        public virtual T this[string key]
        {
            get
            {
                var ret = Get(key);
                if (ret == null)
                    return default(T);
                else
                    return (T)ret;
            }
            set
            {
                Put(key, value);
            }
        }
        public T SafeGet(string key, T defaultValue, string partition = null)
        {
            var value = Get(key);
            if (value != null)
                return (T)value;
            else
                return defaultValue;
        }
        public void Delete(string key, string partition = null)
        {
            ProviderDelete(key);
        }
        public List<KeyValuePair<string,T>> GetAll(string partition = null, Type typeHint = null)
        {
            var list = from item in ProviderGetAll(partition, typeHint)
                       select new KeyValuePair<string, T>(item.Key, (T)item.Value);
            return list.ToList();
        }
        public void BulkPut(List<KeyValuePair<string,T>> values, string partition = null)
        {
            var list = from value in values
                       select new KeyValuePair<string, object>(value.Key, value.Value);
            BulkPut(list, partition);
        }
        public void Clear()
        {
            ProviderClear();
        }
    }
}
